package com.cn.user.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.cn.shiro.TokenManager;
import com.cn.shiro.entity.UUser;
import com.cn.user.entity.MyUser;
import com.cn.user.entity.Poker;
import com.cn.user.service.PokerService;
import com.cn.user.service.UserService;
import com.github.pagehelper.PageHelper;

import redis.clients.jedis.Jedis;

//@RestController注解相当于@ResponseBody ＋ @Controller合在一起的作用。
@Controller
@RequestMapping("homepage")
public class HomePageController {
	
	@Resource
	private UserService userService;
	
	@Resource
	private PokerService pokerService;

	@RequestMapping("selectUsers")
	@ResponseBody
	public List<MyUser> selectUsers(Integer pageNum) {
		if(pageNum==null||pageNum==null){
			pageNum = 1;
		}
		PageHelper.startPage(pageNum,3);  //显示第一页2条数据
		MyUser myUser = new MyUser();
		List<MyUser> selectUser = userService.selectUser(myUser);
		return selectUser;
	}
	@RequestMapping("index")
	public String index(Model model,String name) {
		model.addAttribute("name", name);
		return "index/index";
	}
	@RequestMapping("selectCurUser")
	@ResponseBody
	public UUser selectCurUser() {
		UUser uUser = TokenManager.getToken();
		return uUser;
	}
	
	
	@RequestMapping("fapai")
	public String fapai(Model model) {
		//获取当前用户
		String user_account = TokenManager.getUser_account();
		System.out.println("登陆用户账号："+user_account);
		//查询三个用户
		MyUser myUser = new MyUser();
		List<MyUser> selectUser = userService.selectUser(myUser);
		System.out.println("用户个数："+selectUser.size());
		//查询牌型
		Poker poker = new Poker();
		List<Poker> selectPoker = pokerService.selectPoker(poker);
		System.out.println("扑克个数："+selectPoker.size());
		
		
		Jedis  jedis = new Jedis("192.168.2.117", 6379, 0);
		
		jedis.auth("123456");
		//一人18个
		// 初始化随机数  
		List<Poker> selectPoker1 = new ArrayList<Poker>();
		List<Poker> selectPoker2 = new ArrayList<Poker>();
		List<Poker> selectPoker3 = new ArrayList<Poker>();
        Random rand = new Random(); 
		 // 遍历整个items数组  
        int p = 0;
        for (int i = 0; i < 54; i++) { 
            // 任意取一个0~size的整数，注意此处的items.size()是变化的，所以不能用前面的size会发生数组越界的异常
        	System.out.println("循环："+(selectPoker.size()-1));
        	int myRand = 0;  
        	if(selectPoker.size()-1==0) {
        		myRand = 0;  
        	}else if(selectPoker.size()-1<0){
        		break;
        	}else {
        		myRand = rand.nextInt(selectPoker.size()-1);  
        	}
        	if(p==0) {
        		//将取出的这个元素放到存放结果的集合中  
        		selectPoker1.add(selectPoker.get(myRand));  
        		//从原始集合中删除该元素防止重复。注意，items数组大小发生了改变  
        		selectPoker.remove(myRand);  
        		if(selectPoker1.size()==18) {
        			jedis.set(selectUser.get(0).getUser_realname(), JSON.toJSONString(selectPoker1));
        			if(user_account==selectUser.get(0).getUser_realname()||user_account.equals(selectUser.get(0).getUser_realname())) {
        				System.out.println("1");
        				model.addAttribute("pokers", selectPoker1);
        			}
        			p++;
        		}
        	}
        	if(p==1) {
        		//将取出的这个元素放到存放结果的集合中  
        		selectPoker2.add(selectPoker.get(myRand));  
        		//从原始集合中删除该元素防止重复。注意，items数组大小发生了改变  
        		selectPoker.remove(myRand);
        		if(selectPoker2.size()==18) {
        			jedis.set(selectUser.get(1).getUser_realname(), JSON.toJSONString(selectPoker2));
        			if(user_account==selectUser.get(1).getUser_realname()||user_account.equals(selectUser.get(1).getUser_realname())) {
        				System.out.println("2");
        				model.addAttribute("pokers", selectPoker2);
        			}
            		p++;
        		}
        	}
        	if(p==2) {
        		//将取出的这个元素放到存放结果的集合中  
        		selectPoker3.add(selectPoker.get(myRand));  
        		//从原始集合中删除该元素防止重复。注意，items数组大小发生了改变  
        		selectPoker.remove(myRand); 
        		if(selectPoker3.size()==18) {
        			jedis.set(selectUser.get(2).getUser_realname(), JSON.toJSONString(selectPoker3));
        			if(user_account==selectUser.get(2).getUser_realname()||user_account.equals(selectUser.get(2).getUser_realname())) {
        				System.out.println("3");
        				model.addAttribute("pokers", selectPoker3);
        			}
            		p++;
        		}
        	}
        } 
        
        jedis.close();
        System.out.println(selectPoker1.size());
        System.out.println(selectPoker2.size());
        System.out.println(selectPoker3.size());
        
		return "index/poker";
	}
	
}
